package com.price.common.recyclerview

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.util.SparseArray
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView

/**
 */
open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val views: SparseArray<View> = SparseArray<View>()

    fun <T : View> getViewOrNull(@IdRes viewId: Int): T? {
        var view = views[viewId]
        if (view == null) {
            view = itemView.findViewById(viewId)
            views.put(viewId, view)
        }
        return if(view!=null) view as T else null
    }

    fun <T : View?> findView(@IdRes viewId: Int): T? {
        return itemView.findViewById(viewId)
    }

    fun setText(@IdRes viewId: Int, value: CharSequence?): BaseViewHolder {
        val tv = getViewOrNull<TextView>(viewId)
        tv?.text = value
        return this
    }

    fun setText(@IdRes viewId: Int, @StringRes resId: Int): BaseViewHolder {
        val tv = getViewOrNull<TextView>(viewId)
        tv?.setText(resId)
        return this
    }

    fun setTextColor(@IdRes viewId: Int, @ColorInt color: Int): BaseViewHolder {
        val tv = getViewOrNull<TextView>(viewId)
        tv?.setTextColor(color)
        return this
    }

    fun setTextColorRes(@IdRes viewId: Int, @ColorRes resId: Int): BaseViewHolder {
        val tv = getViewOrNull<TextView>(viewId)
        tv?.setTextColor(itemView.resources.getColor(resId))
        return this
    }

    fun setImageResource(@IdRes viewId: Int, @DrawableRes imageResId: Int): BaseViewHolder {
        val iv = getViewOrNull<ImageView>(viewId)
        iv?.setImageResource(imageResId)
        return this
    }

    fun setImageDrawable(@IdRes viewId: Int, drawable: Drawable?): BaseViewHolder {
        val iv = getViewOrNull<ImageView>(viewId)
        iv?.setImageDrawable(drawable)
        return this
    }

    fun setImageBitmap(@IdRes viewId: Int, bitmap: Bitmap?): BaseViewHolder {
        val iv = getViewOrNull<ImageView>(viewId)
        iv?.setImageBitmap(bitmap)
        return this
    }

    fun setBackgroundColor(@IdRes viewId: Int, @ColorInt color: Int): BaseViewHolder {
        val view = getViewOrNull<View>(viewId)
        view?.setBackgroundColor(color)
        return this
    }

    fun setBackgroundColorRes(@IdRes viewId: Int, @ColorRes resId: Int): BaseViewHolder {
        val view = getViewOrNull<View>(viewId)
        view?.setBackgroundColor(itemView.resources.getColor(resId))
        return this
    }

    fun setBackgroundResource(@IdRes viewId: Int, @DrawableRes backgroundRes: Int): BaseViewHolder {
        val view = getViewOrNull<View>(viewId)
        view?.setBackgroundResource(backgroundRes)
        return this
    }

    fun setVisible(@IdRes viewId: Int, isVisible: Boolean): BaseViewHolder {
        val view = getViewOrNull<View>(viewId)
        view?.visibility = if (isVisible) View.VISIBLE else View.INVISIBLE
        return this
    }

    fun setGone(@IdRes viewId: Int, isGone: Boolean): BaseViewHolder {
        val view = getViewOrNull<View>(viewId)
        view?.visibility = if (isGone) View.GONE else View.VISIBLE
        return this
    }

    fun setEnabled(@IdRes viewId: Int, isEnabled: Boolean): BaseViewHolder {
        val view = getViewOrNull<View>(viewId)
        view?.isEnabled = isEnabled
        return this
    }
}
